package com.albert.iotcar;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.FrameLayout;

import com.albert.View.GameButton;
import com.albert.View.JoystickView;
import com.albert.WSMqtt.WSMqttService;
import com.albert.model.OperateCarModel;
import com.albert.model.PackageModel;
import com.albert.tool.CarActionType;
import com.albert.tool.JsonUtils;
import com.albert.tool.MessageType;

public class CallActivityMoveCar extends FrameLayout {
    private static final String TAG = "移动小车";
    private String mqttReceiveId = "";
    private WSMqttService wsMqttService;

    public CallActivityMoveCar(Context context) {
        super(context);
        init(context);
    }

    public CallActivityMoveCar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public CallActivityMoveCar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    public void setMqttService(WSMqttService mqttService) {
        this.wsMqttService = mqttService;
    }

    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.activity_call_move_car, this, true);
        GameButton leftRotationButton = findViewById(R.id.leftRotation);
        // 设置动作回调
        leftRotationButton.setOnRepeatListener((isPressed) -> {
            Log.d(TAG, "leftRotation");
            PackageModel baseModel = new PackageModel(MessageType.PHONE_MOVE_CAR, mqttReceiveId,
                    new OperateCarModel(CarActionType.CAR_ACTION_LEFT_ROTATE, isPressed));
            String testMsg = JsonUtils.objectToJSonStr(baseModel);
            wsMqttService.publishMessage(testMsg);
        });
        GameButton leftSlideButton = findViewById(R.id.leftSlide);
        // 设置动作回调
        leftSlideButton.setOnRepeatListener((isPressed) -> {
            Log.d(TAG, "leftSlide");
            PackageModel baseModel = new PackageModel(MessageType.PHONE_MOVE_CAR, mqttReceiveId,
                    new OperateCarModel(CarActionType.CAR_ACTION_LEFT_SLIDE, isPressed));
            String testMsg = JsonUtils.objectToJSonStr(baseModel);
            wsMqttService.publishMessage(testMsg);
        });
        GameButton rightRotationButton = findViewById(R.id.rightRotation);
        // 设置动作回调
        rightRotationButton.setOnRepeatListener((isPressed) -> {
            Log.d(TAG, "rightRotation");
            PackageModel baseModel = new PackageModel(MessageType.PHONE_MOVE_CAR, mqttReceiveId,
                    new OperateCarModel(CarActionType.CAR_ACTION_RIGHT_ROTATE, isPressed));
            String testMsg = JsonUtils.objectToJSonStr(baseModel);
            wsMqttService.publishMessage(testMsg);
        });
        GameButton rightSlideButton = findViewById(R.id.rightSlide);
        // 设置动作回调
        rightSlideButton.setOnRepeatListener((isPressed) -> {
            Log.d(TAG, "rightSlide");
            PackageModel baseModel = new PackageModel(MessageType.PHONE_MOVE_CAR, mqttReceiveId,
                    new OperateCarModel(CarActionType.CAR_ACTION_RIGHT_SLIDE, isPressed));
            String testMsg = JsonUtils.objectToJSonStr(baseModel);
            wsMqttService.publishMessage(testMsg);
        });

        JoystickView joystickViewLeft = findViewById(R.id.joystickView_left);
        joystickViewLeft.setOnMoveListener(new JoystickView.OnMoveListener() {
            @Override
            public void onMove(int angle, int strength, int offsetPercentX, int offsetPercentY, int x, int y) {
                Log.d(TAG, String.format("%02d%%  %02d%%:%02d%%  x%03d:y%03d", angle, offsetPercentX, offsetPercentY, x, y));
                PackageModel baseModel = new PackageModel(MessageType.PHONE_MOVE_CAR, mqttReceiveId, new OperateCarModel(offsetPercentX, offsetPercentY));
                String testMsg = JsonUtils.objectToJSonStr(baseModel);
                wsMqttService.publishMessage(testMsg);
            }
        });

        JoystickView joystickViewRight = findViewById(R.id.joystickView_right);
        joystickViewRight.setOnMoveListener(new JoystickView.OnMoveListener() {
            @Override
            public void onMove(int angle, int strength, int offsetPercentX, int offsetPercentY, int x, int y) {
                Log.d(TAG, String.format("%02d%%  %02d%%:%02d%%  x%03d:y%03d", angle, offsetPercentX, offsetPercentY, x, y));
            }
        });
    }
}
